<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <commponts :node-data="companyInfo" :is-company="true" />
        <hr>
        <el-tree :data="depts" :default-expand-all="true" :props="{label:'name'}">
          <!-- 这里是作用域插槽, 由于我们传入一个数组, 遍历都在子组件内部进行
          每个部门遍历到的数据对象, 需要由element 子组件回传过来
          变量名 data 和其中的数据, 都是固定(查文档即可) -->
          <!-- <template v-slot:default="{data}"> -->
          <!-- <template slot="default" slot-scope="{data}"> -->
          <template #default="{data}">
            <commponts :node-data="data" @showWrite="showWrite" @showAddDialog="showAddDialog" @delDepartment="getDepartmentList" />
          </template>
        </el-tree>
      </el-card>
      <adddepartment
        ref="addForm"
        :is-show-dialog.sync="isShowDialog"
        :node-data="nodeData"
        @addDepartment="getDepartmentList"
        @colseIsshowDialog="isShowDialog=false"
      />
    </div>
  </div>
</template>

<script>
import commponts from './components/commponts.vue'
import { getDepartmentList } from '@/api/department'
import { listToTreeData } from '@/utils/index'
import Adddepartment from './components/adddepartment.vue'
export default {
  components: { commponts, Adddepartment },
  data() {
    return {
      companyInfo: {
        name: '黑马程序员',
        manager: '负责人'
      },
      depts: [],
      // 新建弹窗显示与否的控制
      isShowDialog: false,
      nodeData: {}
    }
  },
  async created() {
    // const res = await getDepartmentList()
    // // console.log(res, 'res40')
    // // this.depts = res.depts
    // this.depts = listToTreeData(res.depts, '')
    this.getDepartmentList()
  },
  methods: {
    showAddDialog(nodeData) {
      this.nodeData = nodeData
      this.isShowDialog = true
    },
    // 编辑
    showWrite(nodeData) {
      // 这里赋值只是把数据存储在nodeData中
      // 并没有立即通过vue机制父传子给子组件，它是最后才加载的，所以这是子组件的nodeData为空
      this.nodeData = nodeData
      this.$refs.addForm.getDepartment(nodeData.id)
      this.isShowDialog = true
    },

    async getDepartmentList() {
      const res = await getDepartmentList()
      // console.log(res, 'res40')
      // this.depts = res.depts
      // 将扁平的数据传进去, 可以得到嵌套 children 的结果
      this.depts = listToTreeData(res.depts, '')
    }
  }

}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
